package com.xzavier.yunlian_interview.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzavier.yunlian_interview.entity.QuestionItem;
import com.xzavier.yunlian_interview.entity.QuestionRecord;
import com.xzavier.yunlian_interview.mapper.QuestionItemMapper;
import com.xzavier.yunlian_interview.mapper.QuestionRecordMapper;
import com.xzavier.yunlian_interview.service.QuestionRecordService;
import com.xzavier.yunlian_interview.service.ex.QuestionRecordException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionRecordServiceImpl implements QuestionRecordService {
    @Autowired
    private QuestionRecordMapper questionRecordMapper;
    @Autowired
    private QuestionItemMapper questionItemMapper;

    @Override
    public QuestionRecord addQuestionRecord(String userId, String questionId) {
        QueryWrapper<QuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("questionId", questionId);
        QuestionRecord questionRecord = questionRecordMapper.selectOne(queryWrapper);
        if (questionRecord == null) {
            questionRecord = new QuestionRecord();
            questionRecord.setUserId(userId);
            questionRecord.setQuestionId(questionId);
            questionRecord.setCreatedAt(new Date());
            questionRecord.setReadflag(1);
            questionRecordMapper.insert(questionRecord);
            questionRecord = questionRecordMapper.selectOne(queryWrapper);
        }
        return questionRecord;
    }

    @Override
    public void OptQuestionRecord(String userId, String questionId, Integer opt) {
        QueryWrapper<QuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("questionId", questionId);
        QuestionRecord questionRecord = questionRecordMapper.selectOne(queryWrapper);
        if (questionRecord == null) {
            throw new QuestionRecordException("数据不存在");
        }
        questionRecord.setLiked(opt);
        questionRecordMapper.updateById(questionRecord);

    }

    @Override
    public void CollectQuestionRecord(String userId, String questionId, Integer opt) {
        QueryWrapper<QuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("questionId", questionId);
        QuestionRecord questionRecord = questionRecordMapper.selectOne(queryWrapper);
        if (questionRecord == null) {
            throw new QuestionRecordException("数据不存在");
        }
        questionRecord.setCollectFlag(opt);
        questionRecordMapper.updateById(questionRecord);
    }

    @Override
    public List<QuestionItem> getReadFlag(String userId) {
        QueryWrapper<QuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("readflag", 1);
        queryWrapper.ne("liked", 1);
        queryWrapper.ne("collectFlag", 1);
        List<QuestionRecord> questionRecords = questionRecordMapper.selectList(queryWrapper);
        if (questionRecords == null || questionRecords.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<QuestionItem> questionItemQueryWrapper = new QueryWrapper<>();
        questionItemQueryWrapper.in("id", questionRecords.stream().map(QuestionRecord::getQuestionId).collect(Collectors.toList()));
        return questionItemMapper.selectList(questionItemQueryWrapper);

    }

    @Override
    public void deleteQuestionRecord(String userId, String questionId) {
        QueryWrapper<QuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("questionId", questionId);
        questionRecordMapper.delete(queryWrapper);
    }

    @Override
    public void deleteQuestionRecord(String userId) {
        QueryWrapper<QuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.ne("liked", 1);
        queryWrapper.ne("collectFlag", 1);
        questionRecordMapper.delete(queryWrapper);
    }

    @Override
    public List<QuestionItem> getLikedQuestionRecord(String userId) {
        QueryWrapper<QuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("liked", 1);
        List<QuestionRecord> questionRecords = questionRecordMapper.selectList(queryWrapper);
        if (questionRecords == null || questionRecords.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<QuestionItem> questionItemQueryWrapper = new QueryWrapper<>();
        questionItemQueryWrapper.in("id", questionRecords.stream().map(QuestionRecord::getQuestionId).collect(Collectors.toList()));
        return questionItemMapper.selectList(questionItemQueryWrapper);
    }

    @Override
    public List<QuestionItem> getCollectQuestionRecord(String userId) {
        QueryWrapper<QuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("collectFlag", 1);
        List<QuestionRecord> questionRecords = questionRecordMapper.selectList(queryWrapper);
        if (questionRecords == null || questionRecords.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<QuestionItem> questionItemQueryWrapper = new QueryWrapper<>();
        questionItemQueryWrapper.in("id", questionRecords.stream().map(QuestionRecord::getQuestionId).collect(Collectors.toList()));
        return questionItemMapper.selectList(questionItemQueryWrapper);
    }
}
